package com.example.bee_work_3.fragment.system;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import com.example.bee_work_3.R;

import java.util.List;

public class SystemListAdapter extends RecyclerView.Adapter<SystemListAdapter.ViewHolder> {

    private List<String[]> mList;
    private Context mContext;
    private Fragment_System.ClassCallBack mCallBack;
    private int mResource;//R.layout.class_list;

    public SystemListAdapter(Context context, int resource, List<String[]> list, Fragment_System.ClassCallBack classCallBack) {
        this.mList = list;
        this.mContext = context;
        this.mResource = resource;
        this.mCallBack = classCallBack;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(mResource, null);
        ViewHolder viewHoLder = new ViewHolder(view);
        return viewHoLder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHoLder, final int i) {
        String[] item = mList.get(i);
        final boolean[] first = {true};           //第一次启动,好像是刚刚建立监听时，会因为不知明原因，自动调用点击事件
        ArrayAdapter<String> adapter = new ArrayAdapter<>(mContext, android.R.layout.simple_spinner_item, item);
        viewHoLder.spinner.setAdapter(adapter);
        viewHoLder.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (first[0]) {
                    first[0] = false;
                    return;
                }
                mCallBack.onDataChange(i, position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        return;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        public Spinner spinner;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            spinner = itemView.findViewById(R.id.spinner);
        }
    }

}
